//
//  Present.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "Present.h"
#import "StrategyCollectionView.h"
#import "PresentBigCollectionReusableView.h"
#import "SmallCollectionViewCell.h"
#import "BigCollectionViewCell.h"
#import "PresentModel.h"
#import "PresentRightModel.h"
#import "NetworkHandler.h"
#import "TAMacro.h"
@implementation Present
- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}

- (NSMutableArray *)Bigarray{
    if (!_Bigarray) {
        self.Bigarray = [NSMutableArray array];
        
    }
    return _Bigarray;
}


- (NSMutableArray*)namearray{
    if (!_namearray) {
        self.namearray = [NSMutableArray array];
    }
    return _namearray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
        [self foundBigCollectionView];
        [self foundSmallCollectionView];
        [self analysisSmall];
        [self analysisBig];
    }
    return self;
}


- (void)creatSubview:(CGRect)frame{
    
    self.backgroundColor = [UIColor blackColor];
}

- (void)foundSmallCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 5 , SCREEN_WIDTH / 10);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 0, 0, 5); // 内边距
    
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionViewSmall= [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 20, SCREEN_WIDTH / 4 , SCREEN_HEIGHT - SCREEN_WIDTH  / 3 ) collectionViewLayout:flowLayout
                               ];
    self.collectionViewSmall.delegate = self;
    self.collectionViewSmall.dataSource = self;
    self.collectionViewSmall.pagingEnabled = NO;
    self.collectionViewBig.showsVerticalScrollIndicator = NO;
    
    
    [self.collectionViewSmall jxl_setDayMode:^(UIView *view) {
        self.collectionViewSmall.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.collectionViewSmall.backgroundColor = [UIColor grayColor];
        
        
    }
     ];
    
    
    [self.collectionViewSmall registerClass:[SmallCollectionViewCell class] forCellWithReuseIdentifier:@"GuideSmallCell"];
    
    [self addSubview:self.collectionViewSmall];
    
}

- (void)foundBigCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(  SCREEN_WIDTH / 5 , SCREEN_WIDTH / 5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 10); // 内边距
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    flowLayout.headerReferenceSize = CGSizeMake(100, SCREEN_HEIGHT / 10);
    
    self.collectionViewBig = [[UICollectionView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 20, SCREEN_WIDTH / 4 * 3, SCREEN_HEIGHT - SCREEN_WIDTH  / 3 ) collectionViewLayout:flowLayout];
    self.collectionViewBig.delegate = self;
    self.collectionViewBig.dataSource = self;
    self.collectionViewBig.pagingEnabled = NO;
    self.collectionViewBig.showsVerticalScrollIndicator = NO;
    
    
    [self.collectionViewBig jxl_setDayMode:^(UIView *view) {
        self.collectionViewBig.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.collectionViewBig.backgroundColor = [UIColor grayColor];
        
        
    }
     ];
    
    
    [self addSubview:_collectionViewBig];
    // 注册cell
    [self.collectionViewBig registerClass:[BigCollectionViewCell class] forCellWithReuseIdentifier:@"GuideBigCell"];
    
    [self.collectionViewBig registerClass:[PresentBigCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BigheadView"];
    
}


- (void)analysisSmall{
    
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/item_categories/tree" BodyString:nil WithDataBlock:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"categories"];
        for (NSDictionary *dic  in array) {
            
            PresentModel *model = [[PresentModel alloc]init];
            
            
            model.name = [dic objectForKey:@"name"];
            
            [self.array addObject:model];
            
        }
        
        [self.collectionViewSmall reloadData];
    }];
    
    
    
}

- (void)analysisBig{
    
    
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/item_categories/tree" BodyString:nil WithDataBlock:^(id result) {
        
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"categories"];
        
        for (NSDictionary *dic1 in array) {
            
            NSArray *arr = [dic1 objectForKey:@"subcategories"];
            
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *dic2 in arr) {
                PresentRightModel *model = [PresentRightModel modelWithDic:dic2];
                [tempArray addObject:model];
                
                
            }
            [self.Bigarray addObject:tempArray];
            
        }
        
        [self.collectionViewBig reloadData];
        
    }];
    
    
    
}




- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    if (collectionView == self.collectionViewSmall) {
        
        return self.array.count;
        
    }else{
        
        return [[self.Bigarray objectAtIndex:section] count];
    }
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    if (collectionView == self.collectionViewSmall) {
        return 1;
    }else{
        return self.Bigarray.count;
    }
    
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (collectionView == self.collectionViewSmall) {
        SmallCollectionViewCell *GuideSmallCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideSmallCell" forIndexPath:indexPath];
        if (self.array.count != 0) {
            
            PresentModel*model = self.array[indexPath.item];
            
            [self.namearray addObject:model.name];
            GuideSmallCell.presentModel = model;
            
        }
        
        return GuideSmallCell;
    }else{
        BigCollectionViewCell *GuideBigCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuideBigCell" forIndexPath:indexPath];
        
        if (self.Bigarray.count != 0) {
            
            PresentRightModel *model = [[self.Bigarray objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
            GuideBigCell.presentRightModel = model;
            
            
        }
        
        
        return GuideBigCell;
    }
    
    
    
}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    
    
    NSInteger i = self.collectionViewBig.contentOffset.y / SCREEN_WIDTH  ;
    
    for (int j= 0; j < 17; j ++) {
        if (scrollView == self.collectionViewBig && i == j) {
            [self.collectionViewSmall setContentOffset:CGPointMake(0, (SCREEN_HEIGHT / 17)* j) animated:YES];
            
        }
    }
    
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    for (NSInteger i = 0; i < 17; i++) {
        if (collectionView == self.collectionViewSmall && indexPath.row == i) {
            [self.collectionViewBig setContentOffset:CGPointMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH  / 1.2) * i) animated:YES];
            
        }
    }
    
    
    if (collectionView == self.collectionViewBig) {
        
        PresentRightModel *model = [[self.Bigarray objectAtIndex:indexPath.section] objectAtIndex:indexPath.item];
        
        self.str = [NSString stringWithFormat:@"%@",model.Id];
        [self.delegate didClickSelectItemAtIndexPathPresent:indexPath str:self.str];
        
    }

    
}


- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    PresentBigCollectionReusableView *BigheadView= [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BigheadView" forIndexPath:indexPath];
    
    
    
    //    BigheadView.lableTitle.text = model.name;
    //
    for (int i =0; i<self.namearray.count; i++) {
        BigheadView.lableTitle.text =self.namearray[indexPath.section];
    }
    
    
    
    [BigheadView jxl_setDayMode:^(UIView *view) {
        BigheadView.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        BigheadView.backgroundColor = [UIColor grayColor];
        
        
    }
     ];
    
    
    
    
    return BigheadView;
    
}


@end
